﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using VuzixSample.model.snes.starfox;
using VuzixSample.model.snes;
using System.Diagnostics;

namespace VuzixSample.model.geometry
{
    class Arwing : IGeometry
    {
        private VertexBuffer vertices;
        private IndexBuffer indices;

        private int numberOfVerts;
        private int numberOfIndices;

        public void InitialiseBuffers(StarFoxGeometry fromGeometry, GraphicsDeviceManager usingContext)
        {
            CreateArwingVertexBuffer(fromGeometry.vertices, fromGeometry.faces, usingContext);
            CreateArwingIndexBuffer(fromGeometry.faces, usingContext);
        }
        private void CreateArwingVertexBuffer(List<Vector3> fromVerts, List<SFGFaceGroup> fromFaces,
            GraphicsDeviceManager usingContext)
        {
            numberOfVerts = fromVerts.Count;

            VertexPositionColor[] arwingVertices = new VertexPositionColor[numberOfVerts];

            Debug.WriteLine("vertices");
            //get the vert positions from the verts list
            for (int v = 0; v < numberOfVerts; v++)
            {
                arwingVertices[v].Position = fromVerts[v];
                Debug.WriteLine("{0} : {1}", v, fromVerts[v]);
            }
            
            //get the colours from the faces list
            for (int f = 0; f < fromFaces.Count; f++)
            {
                for (int v = 0; v < fromFaces[f].vertexIndeces.Count; v++)
                {
                    int indexedColour = (int)fromFaces[f].colour;
                    uint vertexIndex  = fromFaces[f].vertexIndeces[v];
                    arwingVertices[vertexIndex].Color = Color.White;//StarFoxPallettes.getColourForIndex(indexedColour);
                }
            }

            vertices = new VertexBuffer(usingContext.GraphicsDevice, VertexPositionColor.VertexDeclaration, numberOfVerts, BufferUsage.WriteOnly);
            vertices.SetData<VertexPositionColor>(arwingVertices);
        }
        private void CreateArwingIndexBuffer(List<SFGFaceGroup> fromFaces,
            GraphicsDeviceManager usingContext)
        {
            /* this should work with the Arwing which has triangle geometry
             * however, there are other geometries in starfox which will need
             * primitive types creating eg 4 vertex plane */

            Debug.WriteLine("indices");
            List<UInt16> serialisedVerts = new List<UInt16>();
            //get the indices from the faces list
            for (int f = 0; f < fromFaces.Count; f++)
                for (int v = 0; v < fromFaces[f].vertexIndeces.Count; v++)
                {
                    serialisedVerts.Add((UInt16)fromFaces[f].vertexIndeces[v]);
                    Debug.WriteLine(serialisedVerts.Last());
                }

            numberOfIndices = serialisedVerts.Count;
            UInt16[] arwingIndices = serialisedVerts.ToArray();

            indices = new IndexBuffer(usingContext.GraphicsDevice, IndexElementSize.SixteenBits, numberOfIndices, BufferUsage.WriteOnly);
            indices.SetData<UInt16>(arwingIndices);
        }
        public void InitialiseBuffers(float scale, GraphicsDeviceManager usingContext)
        {
            //overloaded above this is a stub...
        }
        public void Render(GraphicsDevice usingContext)
        {
            usingContext.SetVertexBuffer(vertices);
            usingContext.Indices = indices;

            //render as seperate triangles
            usingContext.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 
                numberOfVerts, 0, numberOfIndices / 3);
        }
        public void Dispose()
        {
            indices.Dispose();
            vertices.Dispose();
        }
    }
}
